package com.cityfood.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cityfood.common.pojo.CityAndStations;
import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.pojo.CfCity;
import com.cityfood.pojo.CfStation;
import com.cityfood.service.CityService;
import com.cityfood.service.StationService;
import com.github.pagehelper.PageInfo;

@Controller
public class CityController {
	
	@Autowired
	private CityService cityService;
	
	@Autowired
	private StationService stationService;

	/**
	 * 获取城市列表根据城市名称模糊查询
	 */
	@RequestMapping(value="/manage/allCitys",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getAllCitys(){
		CityFoodResult cityfoodResult = new CityFoodResult();
		List<CfCity> citys = cityService.getAllCitys();
		//要返回的集合
		List<Map<String,Object>> ls = new ArrayList<>(); 
		//遍历城市获取id
		for (CfCity city : citys) {
			if (city.getIsdisplay()!=0) {
				//包装要展示的数据
				Map<String,Object> ms = new LinkedHashMap<String,Object>();
				ms.put("cityId", city.getCityId());
				ms.put("cityName", city.getCityName());
				ms.put("isHot",city.getIshot());   //增加热门城市标识
				ls.add(ms);
				
			}
		}
		if (ls.size()>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("获取所有城市列表成功");
			cityfoodResult.setData(ls);
			return cityfoodResult;
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("暂无城市信息");
			cityfoodResult.setData("{}");
			return cityfoodResult;
		}
	}
	
	/**
	 * 获取城市列表根据城市名称模糊查询
	 */
	@RequestMapping(value="/citys",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getCityByCityName(@RequestParam(defaultValue="")String cityName){
		CityFoodResult cityfoodResult = new CityFoodResult();
		List<CfCity> citys = cityService.getCityLikeCityName(cityName);
		//要返回的集合
		List<Map<String,Object>> ls = new ArrayList<>(); 
		//遍历城市获取id
		for (CfCity city : citys) {
			if (city.getIsdisplay()!=0) {
				List<CfStation> stations = stationService.getStationByCityId(city.getCityId());
				//添加isDisplay不等于0的到新集合里面
				List<CfStation> s = new ArrayList<>();
				for (CfStation cfStation : stations) {
					if (cfStation.getDisplay()!=0) {
						s.add(cfStation);
					}
				}
				
				//包装要展示的数据
				Map<String,Object> ms = new LinkedHashMap<String,Object>();
				ms.put("cityId", city.getCityId());
				ms.put("cityName", city.getCityName());
				ms.put("isHot",city.getIshot());   //增加热门城市标识
				ms.put("stations", s);
				ls.add(ms);
				
			}
		}
		if (ls.size()>0) {
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("获取城市列表成功");
			cityfoodResult.setData(ls);
			return cityfoodResult;
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("暂无城市信息");
			cityfoodResult.setData("{}");
			return cityfoodResult;
		}
	}
	
	/**
	 * 定位城市
	 */
	@RequestMapping(value="/citys/{id}",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getCityByCityId(@PathVariable(value="id") Integer cityId){
		CityFoodResult cityfoodResult = new CityFoodResult();
		CfCity city = cityService.getCityByCityId(cityId);
		if (city==null) {
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("定位失败，请检查网络，或者GPS是否打开");
			cityfoodResult.setData("{}");
			return cityfoodResult;
		}
		if (city.getIsdisplay()==0) {
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("暂无城市信息");
			cityfoodResult.setData("{}");
			return cityfoodResult;
		}
		List<CfStation> stations = stationService.getStationByCityId(cityId);
		//城市高铁站包装类
		CityAndStations cityAndStations = new CityAndStations();
		// 添加包装类的属性
		cityAndStations.setCityId(city.getCityId());
		cityAndStations.setCityName(city.getCityName());
		cityAndStations.setStations(stations);
		cityfoodResult.setCode(200);
		cityfoodResult.setMsg("定位城市成功");
		cityfoodResult.setData(cityAndStations);
		return cityfoodResult;
	}
	
	/**
	 * 获取城市详情，根据城市id
	 */
	@RequestMapping(value="/citys/info",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getCityInformationByCityId(Integer cityId){
		CityFoodResult cityfoodResult = new CityFoodResult();
		CfCity city = cityService.getCityByCityId(cityId);
		if (city.getIsdisplay()==0) {
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("暂无城市信息");
			cityfoodResult.setData("{}");
			return cityfoodResult;
		}
		if (city!=null) {
			Map<String,Object> ms = new LinkedHashMap<String,Object>();
			ms.put("title", city.getTitle());
			ms.put("content", city.getContent());
			//数据库path为逗号隔开的字符串
			String[] strs = city.getPath().split(",");
			List<String> paths = new ArrayList<>();
			for (String string : strs) {
				paths.add(string);
			}
			ms.put("path", paths);
			ms.put("type", city.getType());
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("获取城市信息成功");
			cityfoodResult.setData(ms);
			return cityfoodResult;
		}
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("暂无城市信息");
			cityfoodResult.setData("{}");
			return cityfoodResult;
	}
	
	
	//后台管理---------------------------------------------------------------------------
	
	/**
	 * 获取城市列表根据城市名称模糊查询
	 */
	@RequestMapping(value="/manage/citys",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getCityLikeName(@RequestParam(defaultValue="")String cityName,
										  @RequestParam(defaultValue="1")Integer currentPage){
		
		CityFoodResult cityfoodResult = new CityFoodResult();
		PageInfo<CfCity> citys = cityService.getCityLikeCityNamePage(cityName,currentPage);
		
		//遍历城市获取id
		if (citys.getTotal()>0) {
			
			Map<String,Object> mp = new LinkedHashMap<String, Object>();
			//要返回的集合
			List<Map<String,Object>> ls = new ArrayList<Map<String,Object>>(); 
			for (CfCity city : citys.getList()) {
//				包装要展示的数据
				Map<String,Object> ms = new LinkedHashMap<String,Object>();
				ms.put("cityId", city.getCityId());
				ms.put("cityName", city.getCityName());
				ms.put("isHot", city.getIshot());
				ms.put("isDisplay", city.getIsdisplay());
				ms.put("title", city.getTitle());
				ms.put("content", city.getContent());
				ms.put("path", city.getPath());
				ms.put("details", city.getDetails());
				ms.put("type", city.getType());
				ls.add(ms);
				
			}
			mp.put("total", citys.getTotal());
			mp.put("citys", ls);
			cityfoodResult.setCode(200);
			cityfoodResult.setMsg("获取城市列表成功");
			cityfoodResult.setData(mp);
			return cityfoodResult;
		}else{
			cityfoodResult.setCode(201);
			cityfoodResult.setMsg("暂无城市信息");
			cityfoodResult.setData("{}");
			return cityfoodResult;
		}
	}
	
	/**
	 * 添加城市
	 */
	@RequestMapping(value="/manage/citys",method=RequestMethod.POST)
	@ResponseBody
	public CityFoodResult insertCity(@RequestBody Map<String,Object> map){
		
		
		String cityName = (String)map.get("cityName");
		String title = (String)map.get("title");
		String content = (String)map.get("content");
		String path = (String)map.get("path");
		String details = (String)map.get("details");
		Byte isHot = 1;
		if (map.containsKey("isHot")) {
			isHot = Byte.parseByte(map.get("isHot").toString());
		}
		Byte type = 1;
		if (map.containsKey("type")) {
			isHot = Byte.parseByte(map.get("type").toString());
		}
		Byte isDisplay = 1;
		if (map.containsKey("isDisplay")) {
			isHot = Byte.parseByte(map.get("isDisplay").toString());
		}
		
		
		CityFoodResult cityFoodResult = new CityFoodResult();
		CfCity city=new CfCity();
		city.setCityName(cityName);
		city.setTitle(title);
		city.setContent(content);
		city.setPath(path);
		city.setDetails(details);
		city.setIshot(isHot);
		city.setType(type);
		city.setIsdisplay(isDisplay);
		int i = cityService.insertCity(city);
		
		if (i>0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("添加城市信息成功");
			cityFoodResult.setData(true);
		}else{
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("添加城市信息失败");
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}
	
	/**
	 * 修改城市信息
	 */
	@RequestMapping(value="/manage/citys",method=RequestMethod.PUT)
	@ResponseBody
	public CityFoodResult updateCity(@RequestBody Map<String,Object> map){
		
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		Integer cityId = Integer.parseInt(map.get("cityId").toString());
		String cityName = (String)map.get("cityName");
		String title = (String)map.get("title");
		String content = (String)map.get("content");
		String path = (String)map.get("path");
		String details = (String)map.get("details");
		Byte isHot = 1;
		if (map.containsKey("isHot")) {
			isHot = Byte.parseByte(map.get("isHot").toString());
		}
		Byte type = 1;
		if (map.containsKey("type")) {
			isHot = Byte.parseByte(map.get("type").toString());
		}
		Byte isDisplay = 1;
		if (map.containsKey("isDisplay")) {
			isHot = Byte.parseByte(map.get("isDisplay").toString());
		}
		
		CfCity city = new CfCity();
		city.setCityId(cityId);
		city.setCityName(cityName);
		city.setTitle(title);
		city.setContent(content);
		city.setPath(path);
		city.setDetails(details);
		city.setIshot(isHot);
		city.setType(type);
		city.setIsdisplay(isDisplay);
		int i = cityService.updateCity(city);
		if (i>0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("修改城市成功");
			cityFoodResult.setData(true);
		}else{
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("修改城市失败");
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}
	
	/**
	 * 删除城市信息,单独删除
	 */
	@RequestMapping(value="/manage/citys/remove",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteCity(@RequestBody Map map){
	    String id = map.get("id").toString();
		CityFoodResult cityFoodResult = new CityFoodResult();
			CfCity city = new CfCity();
			city.setCityId(Integer.parseInt(id));
			int i = cityService.deleteCity(city);
			if (i>0) {
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("删除城市成功");
				cityFoodResult.setData(true);
			}else{
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("删除城市失败");
				cityFoodResult.setData(false);
				return cityFoodResult;
			}
		return cityFoodResult;
	}
	
	/**
	 * 删除城市信息，多删
	 */
	@RequestMapping(value="/manage/citys/batchremove",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteCitys(@RequestBody Map map){
	    String ids = map.get("ids").toString();
		CityFoodResult cityFoodResult = new CityFoodResult();
		String[] strIds = ids.split(",");
		//根据城市id查询，没有值时为空
		for (String strId : strIds) {
			Integer cityId = Integer.parseInt(strId);
			CfCity city = new CfCity();
			city.setCityId(cityId);
			int i = cityService.deleteCity(city);
			if (i>0) {
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("删除城市成功");
				cityFoodResult.setData(true);
			}else{
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("删除城市失败");
				cityFoodResult.setData(false);
				return cityFoodResult;
			}
		}
		return cityFoodResult;
	}

}
